package com.shawn.server.controller;


import com.shawn.commons.utils.CommentType;
import com.shawn.commons.utils.RC;
import com.shawn.model.vo.CommentsVo;
import com.shawn.model.vo.PageResult;
import com.shawn.model.vo.VideoVo;
import com.shawn.server.interceptor.ThreadContext;
import com.shawn.server.service.FreezeService;
import com.shawn.server.service.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/smallVideos")
public class ShortVideosController {


    @Autowired
    private VideoService videoService;

    @Autowired
    private FreezeService freezeService;

    /*/smallVideos/comments/:id/like*/
    /**
     * 根据评论id对评论进行点赞
     *
     * @return {@link ResponseEntity}
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity commentLike(@PathVariable String id){
        Long userId = ThreadContext.getUserId();
        Integer likeCount = videoService.commentWhetherLike(userId,id,CommentType.LIKE.getType());
        return ResponseEntity.ok(likeCount);
    }
    /**
     * 根据评论id对评论进行取消点赞
     *
     * @return {@link ResponseEntity}
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity commentDisLike(@PathVariable String id){
        Long userId = ThreadContext.getUserId();
        Integer likeCount = videoService.commentWhetherLike(userId,id,CommentType.DISLIKE.getType());
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 分页得到所有评论
     *
     * @param videoId  视频id
     * @param page     页面
     * @param pageSize 页面大小
     * @return {@link ResponseEntity}
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity getAllComments(@PathVariable(name = "id") String videoId, @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(name = "pagesize",defaultValue = "10") Integer pageSize){
        PageResult<CommentsVo> pageResult = videoService.getAllComments(videoId,page,pageSize);
        return ResponseEntity.ok(pageResult);
    }


    /**
     * 取消关注作者
     *
     * @param uid 用户id
     * @return {@link ResponseEntity}
     *//*/smallVideos/:uid/userFocus*/
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity cancelFocusAuthor(@PathVariable Long uid){
        Long userId = ThreadContext.getUserId();
        videoService.whetherFocusAuthor(userId,uid,CommentType.CANCEL_FOCUS.getType());
        return ResponseEntity.ok(true);
    }
    /**
     * 关注作者
     *
     * @param uid 用户id
     * @return {@link ResponseEntity}
     *//*/smallVideos/:uid/userFocus*/
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity focusAuthor(@PathVariable Long uid){
        Long userId = ThreadContext.getUserId();
        videoService.whetherFocusAuthor(userId,uid,CommentType.FOCUS.getType());
        return ResponseEntity.ok(true);
    }


    /**
     * 发表评论
     *
     * @param id  id
     * @param map 地图
     * @return {@link ResponseEntity}
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity publishComments(@PathVariable String id, @RequestBody Map<String, String> map) {

        freezeService.checkFreeze(ThreadContext.getUserId(), RC.FREEZING_COMMENT);
        //获取信息
        String comment = map.get("comment");
        Long userId = ThreadContext.getUserId();
        Integer commentCount = videoService.publishComment(userId, id, comment);
        return ResponseEntity.ok(commentCount);
    }

    /**
     * 视频点赞
     *
     * @param id id
     * @return {@link ResponseEntity}
     */
    @CacheEvict(value = "recommendVideo",allEntries = true) //视频点赞后删除所有视频缓存
    @PostMapping("/{id}/like")
    public ResponseEntity likeVideo(@PathVariable String id) {
        Integer likeCount = videoService.whetherLikeVideo(id, CommentType.LIKE.getType());
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 视频取消点赞
     *
     * @param id id
     * @return {@link ResponseEntity}
     */

    @PostMapping("/{id}/dislike")
    public ResponseEntity disLikeVideo(@PathVariable String id) {
        Integer likeCount = videoService.whetherLikeVideo(id, CommentType.DISLIKE.getType());
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 发布视频
     *
     * @param videoThumbnail 视频缩略图
     * @param videoFile      视频文件
     * @return {@link ResponseEntity}
     */

    @PostMapping
    public ResponseEntity publishVideo(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        Long userId = ThreadContext.getUserId();
        videoService.publishVideo(userId, videoThumbnail, videoFile);
        return ResponseEntity.ok("视频上传成功!");
    }

    /**
     * 推荐视频
     *
     * @param page     页面
     * @param pageSize 页面大小
     * @return {@link ResponseEntity}
     *///userid _ page_pagesize

    @GetMapping
    public ResponseEntity recommendVideo(@RequestParam(defaultValue = "1") Integer page, @RequestParam(name = "pagesize", defaultValue = "10") Integer pageSize) {
        //todo 查询redis推荐数据,为空再查询视频列表
        Long userId = ThreadContext.getUserId();
        PageResult<VideoVo> pageResult = videoService.recommendVideo(userId, page, pageSize);
        return ResponseEntity.ok(pageResult);
    }

}
